#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Prsht.h>
#include <CommCtrl.h>
#include "resource.h"


wchar_t szClassName[] = L"WindowsApp";
HWND hMainWindow = NULL;

wchar_t g_achTemp[256];
HWND hwndTab; 


LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_SIZE:
		{
			MoveWindow(hwndTab, 0, 0, LOWORD(lParam), HIWORD(lParam), true);
		}

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case ID_MAIN_FILE_EXIT:
			DestroyWindow(hwnd);
			return 0;
		}
	}

	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
/*
BOOL CALLBACK FontDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

BOOL CALLBACK BorderDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void createTabs(HINSTANCE g_hinst, HWND hwndOwner)
{
	PROPSHEETPAGE psp[2];
	PROPSHEETHEADER psh;
	psp[0].dwSize = sizeof(PROPSHEETPAGE);
	psp[0].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[0].hInstance = g_hinst;
	psp[0].pszTemplate = MAKEINTRESOURCE(IDD_DIALOG1);
	psp[0].pszIcon = NULL;//MAKEINTRESOURCE(IDI_FONT);
	psp[0].pfnDlgProc = FontDialogProc;
	psp[0].pszTitle = L"Test1";//MAKEINTRESOURCE(IDS_FONT)
	psp[0].lParam = 0;
	psp[0].pfnCallback = NULL;

	psp[1].dwSize = sizeof(PROPSHEETPAGE);
	psp[1].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[1].hInstance = g_hinst;
	psp[1].pszTemplate = MAKEINTRESOURCE(IDD_DIALOG2);
	psp[1].pszIcon = NULL;//MAKEINTRESOURCE(IDI_BORDER);
	psp[1].pfnDlgProc = BorderDialogProc;
	psp[1].pszTitle = L"Test2";//MAKEINTRESOURCE(IDS_BORDER);
	psp[1].lParam = 0;
	psp[1].pfnCallback = NULL;

	psh.dwSize = sizeof(PROPSHEETHEADER);
	psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE;
	psh.hwndParent = hwndOwner;
	psh.hInstance = g_hinst;
	psh.pszIcon = NULL;//MAKEINTRESOURCE(IDI_CELL_PROPERTIES);
	psh.pszCaption = L"Cell Properties";
	psh.nPages = sizeof(psp) /	sizeof(PROPSHEETPAGE);
	psh.nStartPage = 0;
	psh.ppsp = (LPCPROPSHEETPAGE) &psp;
	psh.pfnCallback = NULL;
	PropertySheet(&psh);

}
*/


HWND WINAPI DoCreateTabControl(HWND hwndParent, HINSTANCE g_hinst) 
{ 
	RECT rcClient; 
	TCITEM tie; 
	int i; 

	// Get the dimensions of the parent window's client area, and 
	// create a tab control child window of that size. 
	GetClientRect(hwndParent, &rcClient); 
	InitCommonControls(); 
	hwndTab = CreateWindow(WC_TABCONTROL, L"", 
		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 
		0, 0, rcClient.right, rcClient.bottom, 
		hwndParent, NULL, g_hinst, NULL); 
	if (hwndTab == NULL)
	{ 
		return NULL; 
	}

	// Add tabs for each day of the week. 
	tie.mask = TCIF_TEXT | TCIF_IMAGE; 
	tie.iImage = -1; 
	tie.pszText = g_achTemp; 

	for (i = 0; i < 7; i++) 
	{ 
		LoadString(g_hinst, IDS_FIRSTDAY + i, 
			g_achTemp, sizeof(g_achTemp)/sizeof(g_achTemp[0])); 
		if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1) 
		{ 
			DestroyWindow(hwndTab); 
			return NULL; 
		} 
	} 
	return hwndTab; 
} 


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreviousInstance, LPSTR lpCommandLine, int nShowCommand)  
{
	MSG messages;
	WNDCLASSEX wcex;
	HACCEL   hAccel;


	wcex.cbClsExtra = 0;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.cbWndExtra = 0;
	wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hInstance = hInstance;
	wcex.lpfnWndProc = WindowProcedure;
	wcex.lpszClassName = szClassName;
	wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU_MAIN);
	wcex.style = CS_HREDRAW | CS_VREDRAW;

	if (!RegisterClassEx(&wcex))
		return 0;

	hMainWindow = CreateWindowEx(0, szClassName, L"Win32 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, HWND_DESKTOP, NULL, hInstance, NULL);																																																								 

	ShowWindow(hMainWindow, nShowCommand);
	UpdateWindow(hMainWindow);

	hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(IDR_MAIN_ACC));


//	createTabs(hInstance, hMainWindow);
	DoCreateTabControl(hMainWindow, hInstance);

	while(GetMessage(&messages, NULL, 0, 0))
	{
		if (!TranslateAccelerator(hMainWindow, hAccel, &messages))
		{
			TranslateMessage(&messages);
			DispatchMessage(&messages);
		}
	}

	return messages.wParam;
}